package com.xbongbong.saas.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.constant.SizeConstant;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.elasticsearch.pojo.XbbAggregatedPage;
import com.xbongbong.paas.elasticsearch.util.EsHelper;
import com.xbongbong.paas.elasticsearch.util.EsUtil;
import com.xbongbong.paas.elasticsearch.util.XbbElasticsearchRestTemplate;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.FlowStatusEnum;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.paas.toolbox.wrap.BaseVO;
import com.xbongbong.pro.config.ProBaseConfig;
import com.xbongbong.pro.constant.ElasticConstant;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.marketManage.pojo.dto.MarketActivity4StatisticsRecalculateDTO;
import com.xbongbong.pro.marketManage.pojo.dto.MarketActivity4StatisticsSyncContractDTO;
import com.xbongbong.pro.marketManage.pojo.dto.MarketActivityForStaticDTO;
import com.xbongbong.pro.thread.ThreadPoolBeanConfig;
import com.xbongbong.saas.domain.entity.StaticForMarketActivityEntity;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.ClueEnum;
import com.xbongbong.saas.enums.business.ContractEnum;
import com.xbongbong.saas.enums.business.CustomerManagementEnum;
import com.xbongbong.saas.enums.business.MarketActivityEnum;
import com.xbongbong.saas.enums.business.SalesOpportunityEnum;
import com.xbongbong.saas.help.workflow.MarketActivityForStaticHelp;
import com.xbongbong.saas.model.IndexTypeModel;
import com.xbongbong.saas.model.MarketActivityForStaticModel;
import com.xbongbong.saas.model.MarketActivityModel;
import com.xbongbong.saas.service.MarketActivityForStaticService;
import com.xbongbong.saas.toolbox.help.FastJsonHelper;
import com.xbongbong.sys.model.CompanyModel;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
import static org.elasticsearch.index.query.QueryBuilders.rangeQuery;
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
import static org.elasticsearch.index.query.QueryBuilders.termsQuery;

/**
 * @author guohui.zhang
 * @Title:
 * @date 2020/12/1813:48
 */
@Service("marketActivityForStaticService")
@Slf4j
public class MarketActivityForStaticServiceImpl implements MarketActivityForStaticService {
    private static final Logger LOG = LoggerFactory
            .getLogger(MarketActivityForStaticServiceImpl.class);

    @Resource
    private EsHelper esHelper;
    @Resource
    private MarketActivityForStaticModel marketActivityForStaticModel;
    @Resource
    private MarketActivityModel marketActivityModel;
    @Resource
    private XbbElasticsearchRestTemplate xbbElasticsearchRestTemplate;
    @Resource
    private ProBaseConfig proBaseConfig;
    @Resource
    private MarketActivityForStaticHelp marketActivityForStaticHelp;
    @Resource
    private CompanyModel companyModel;
    @Resource
    private ThreadPoolBeanConfig threadPoolBeanConfig;
    @Resource
    private IndexTypeModel indexTypeModel;

    public static final String LOG_FLAG = "----------------- {} -----------------";

    @Override
    @Deprecated
    public BaseVO staticMarketActivity(MarketActivityForStaticDTO marketActivityForStaticDTO) throws XbbException {
        LOG.info("------------------------- 统计市场活动数据入库 -> 定时任务 开始执行 --------------------------");
        LOG.info("========================= 统计市场活动数据入库 定时任务执行开始时间：" + DateUtil.getInt() + " ==========================");
        if (!proBaseConfig.getXbbCode().equals(marketActivityForStaticDTO.getXbbCode())) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
        }
        List<String> corpidList = marketActivityForStaticDTO.getCorpIdList();
        List<Long> marketIds = marketActivityForStaticDTO.getMarketIds();
        if (corpidList.size() > 1 && marketIds.size() > 1) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
        Long now = DateTimeUtil.getInt();
        for (String corpid : corpidList) {
            BoolQueryBuilder boolQueryBuilder = boolQuery();
            boolQueryBuilder.filter(termQuery("corpid.keyword", corpid));
            boolQueryBuilder.filter(termQuery("del", 0));
            if (Objects.nonNull(marketIds) && marketIds.size() > 0) {
                boolQueryBuilder.filter(termsQuery("dataId", marketIds));
            }
            boolQueryBuilder.filter(termQuery(ElasticConstant.MY_JOIN_FIELD, IndexTypeEnum.IDX_SAAS_MARKET_ACTIVITY.getType()));
            SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
            sourceBuilder.query(boolQueryBuilder);
            SearchRequest searchRequest = new SearchRequest(IndexTypeEnum.IDX_SAAS_MARKET_ACTIVITY.getIndex());
            searchRequest.source(sourceBuilder);
            EsUtil.setPage(sourceBuilder, 1, SizeConstant.PAGE_SIZE);
            List<PaasFormDataEntityExt> marketActivityList = xbbElasticsearchRestTemplate.findScrollList(PaasFormDataEntityExt.class, searchRequest, SizeConstant.SCROLL_TIME_IN_MILLIS);
            List<Long> hasDeleteMarketIds = new ArrayList<>();
            if ( CollectionsUtil.isNotEmpty(marketActivityList) ) {
                IndexTypeEnum customerIndex = indexTypeModel.getCustomerIndex(corpid, XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode(), SaasMarkEnum.SAAS.getCode());

                //线索
                for (PaasFormDataEntityExt marketActivity : marketActivityList) {
                    BoolQueryBuilder cluesBoolQueryBuilder = boolQuery();
                    cluesBoolQueryBuilder.filter(termQuery("corpid.keyword", corpid));
                    cluesBoolQueryBuilder.filter(termQuery("del", 0));
                    cluesBoolQueryBuilder.filter(termQuery(ClueEnum.getEsAttr4Keyword(ClueEnum.MARKET_ACTIVITY_ID), marketActivity.getDataId()));
                    cluesBoolQueryBuilder.filter(termQuery(ElasticConstant.MY_JOIN_FIELD, IndexTypeEnum.IDX_SAAS_CLUE.getType()));
                    SearchSourceBuilder clueSourceBuilder = new SearchSourceBuilder();
                    long idGet = 0;
                    SearchRequest clueSearchRequest = new SearchRequest(IndexTypeEnum.IDX_SAAS_CLUE.getIndex());
                    PageRequest pageRequest = null;
                    int page = 0;
                    Boolean hasMore = true;
                    while (hasMore) {
                        cluesBoolQueryBuilder.filter(rangeQuery("dataId").gte(idGet));
                        clueSourceBuilder.query(cluesBoolQueryBuilder);
                        clueSourceBuilder.sort(new FieldSortBuilder("dataId").order(SortOrder.ASC));
                        pageRequest = EsUtil.setPage(clueSourceBuilder, page, 1000);
                        clueSearchRequest.source(clueSourceBuilder);
                        XbbAggregatedPage<PaasFormDataEntityExt> esEntities = xbbElasticsearchRestTemplate.queryForPages(pageRequest, clueSearchRequest, PaasFormDataEntityExt.class);
                        List<PaasFormDataEntityExt> clueList = esEntities.getContent();
                        Integer count = clueList.size();
                        if (count < 1000) {
                            hasMore = false;
                        }
                        //如果市场活动下没有线索，就只要插入市场活动的数据就好
                        if (count <= 0) {
                            marketActivityForStaticModel.insert(setEntityData(corpid, marketActivity, now));
                            continue;
                        }
                        idGet = clueList.get(count - 1).getDataId();
                        //客户
                        List<Long> clueIdAll = new ArrayList<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                        List<Long> clueIdList = new ArrayList<>(clueList.size());
                        Map<Long, PaasFormDataEntityExt> clueMap = new HashMap<>(clueList.size());
                        for (PaasFormDataEntityExt clue : clueList) {
                            clueIdList.add(clue.getDataId());
                            clueMap.put(clue.getDataId(), clue);
                        }
                        LOG.info("查询出来的线索id为:" + clueIdList.toString());
                        BoolQueryBuilder customerBoolQueryBuilder = boolQuery();
                        customerBoolQueryBuilder.filter(termQuery("corpid.keyword", corpid));
                        customerBoolQueryBuilder.filter(termQuery("del", 0));
                        customerBoolQueryBuilder.filter(termsQuery(CustomerManagementEnum.getEsAttr4Keyword(CustomerManagementEnum.CLUE_ID), clueIdList));
                        customerBoolQueryBuilder.filter(termQuery(ElasticConstant.MY_JOIN_FIELD, customerIndex.getType()));
                        SearchSourceBuilder customerSourceBuilder = new SearchSourceBuilder();
                        long customerIdGet = 0;
                        SearchRequest customerSearchRequest = new SearchRequest(customerIndex.getIndex());
                        PageRequest customerPageRequest = null;
                        int customerPage = 0;
                        Boolean customerHasMore = true;
                        List<StaticForMarketActivityEntity> insertListEntitys = new ArrayList<>();
                        while (customerHasMore) {
                            //客户
                            List<Long> customerIdAll = new ArrayList<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                            customerBoolQueryBuilder.filter(rangeQuery("dataId").gte(customerIdGet));
                            customerSourceBuilder.query(customerBoolQueryBuilder);
                            customerSourceBuilder.sort(new FieldSortBuilder("dataId").order(SortOrder.ASC));
                            customerPageRequest = EsUtil.setPage(customerSourceBuilder, customerPage, 1000);
                            customerSearchRequest.source(customerSourceBuilder);
                            XbbAggregatedPage<PaasFormDataEntityExt> customerEntities = xbbElasticsearchRestTemplate.queryForPages(customerPageRequest, customerSearchRequest, PaasFormDataEntityExt.class);
                            List<PaasFormDataEntityExt> customerList = customerEntities.getContent();
                            Integer customerCount = customerList.size();
                            if (customerCount < 1000) {
                                customerHasMore = false;
                            }
                            if (CollectionsUtil.isEmpty(customerList)) {
                                // 新增无转化客户的线索
                                continue;
                            }
                            customerIdGet = customerList.get(customerCount - 1).getDataId();
                            //机会
                            List<Long> customerIdList = new ArrayList<>(customerList.size());
                            customerIdList.add(-1L);
                            Map<Long, List<Long>> customerClueMap = new HashMap<>(customerList.size());
                            Map<Long, PaasFormDataEntityExt> customerMap = new HashMap<>(customerList.size());
                            for (PaasFormDataEntityExt customerEntityExt : customerList) {
                                Long customerId = customerEntityExt.getDataId();
                                JSONObject customerData = customerEntityExt.getData();
                                JSONArray clueIdArray = FastJsonHelper.getJsonArrOrDefaultFromFormData(customerData, CustomerManagementEnum.CLUE_ID.getAttr(), new JSONArray());
                                customerIdList.add(customerId);
                                List<Long> customerClueList = new ArrayList<>(clueIdArray.size());
                                clueIdArray.forEach(clueIdObject -> {
                                    customerClueList.add(Long.valueOf(clueIdObject.toString()));
                                });
                                customerClueMap.put(customerId, customerClueList);
                                customerMap.put(customerId, customerEntityExt);
                            }
                            LOG.info("查询出来的客戶id为:" + customerIdList.toString());
                            LOG.info("客户与线索对应的关系为:" + customerClueMap.toString());
                            BoolQueryBuilder opportunityBoolQueryBuilder = boolQuery();
                            opportunityBoolQueryBuilder.filter(termQuery("corpid.keyword", corpid));
                            opportunityBoolQueryBuilder.filter(termQuery("del", 0));
                            opportunityBoolQueryBuilder.filter(termsQuery(SalesOpportunityEnum.getEsAttr4Keyword(SalesOpportunityEnum.CUSTOMER_NAME), customerIdList));
                            opportunityBoolQueryBuilder.filter(termQuery(ElasticConstant.MY_JOIN_FIELD, IndexTypeEnum.IDX_SAAS_OPPORTUNITY.getType()));
                            SearchSourceBuilder opportunitySourceBuilder = new SearchSourceBuilder();
                            long opportunityIdGet = 0;
                            SearchRequest opportunitySearchRequest = new SearchRequest(IndexTypeEnum.IDX_SAAS_OPPORTUNITY.getIndex());
                            PageRequest opportunityPageRequest = null;
                            int opportunityPage = 0;
                            Boolean opportunityHasMore = true;
                            while (opportunityHasMore) {
                                opportunityBoolQueryBuilder.filter(rangeQuery("dataId").gte(opportunityIdGet));
                                opportunitySourceBuilder.query(opportunityBoolQueryBuilder);
                                opportunitySourceBuilder.sort(new FieldSortBuilder("dataId").order(SortOrder.ASC));
                                opportunityPageRequest = EsUtil.setPage(opportunitySourceBuilder, opportunityPage, 1000);
                                opportunitySearchRequest.source(opportunitySourceBuilder);
                                XbbAggregatedPage<PaasFormDataEntityExt> opportunityEntities = xbbElasticsearchRestTemplate.queryForPages(opportunityPageRequest, opportunitySearchRequest, PaasFormDataEntityExt.class);
                                List<PaasFormDataEntityExt> opportunityList = opportunityEntities.getContent();
                                Integer opportunityCount = opportunityList.size();
                                if (opportunityCount < 1000) {
                                    opportunityHasMore = false;
                                }
                                if (CollectionsUtil.isEmpty(opportunityList)) {
                                    // insertList 新增数据 机会的客户
                                    continue;
                                }
                                opportunityIdGet = opportunityList.get(opportunityCount - 1).getDataId();
                                //插入数据
                                for (PaasFormDataEntityExt opportunity : opportunityList) {
                                    Long opportunityCustomerId = FastJsonHelper.getLongFromFormData(opportunity.getData(), SalesOpportunityEnum.CUSTOMER_NAME.getAttr());
                                    customerIdAll.add(opportunityCustomerId);
                                    StaticForMarketActivityEntity staticForMarketActivityEntity = setEntityData(corpid, marketActivity, now);
                                    staticForMarketActivityEntity.setCustomerId(opportunityCustomerId);
                                    List<Long> customerClueList = customerClueMap.get(opportunityCustomerId);
                                    PaasFormDataEntityExt customerEntity = customerMap.get(opportunityCustomerId);
                                    if (Objects.nonNull(customerEntity)) {
                                        customerMap.remove(opportunityCustomerId);
                                    }
                                    if (CollectionsUtil.isNotEmpty(customerClueList)) {
                                        LOG.info("销售机会插入数据 看看此时的客户customerClueList:" + customerClueList.toString());
                                        LOG.info("销售机会插入数据看看此时clueMap的值:" + clueMap.keySet().toString());
                                        for (Long clueId : customerClueList) {
                                            clueIdAll.add(clueId);
                                            PaasFormDataEntityExt clueDataEntityExt = clueMap.get(clueId);
                                            if (Objects.nonNull(clueDataEntityExt)) {
                                                StaticForMarketActivityEntity staticForMarketActivityEntity1 = new StaticForMarketActivityEntity();
                                                BeanUtil.copyProperties(staticForMarketActivityEntity, staticForMarketActivityEntity1);
                                                staticForMarketActivityEntity1.setClueId(clueId);
                                                staticForMarketActivityEntity1.setClueStatus(FastJsonHelper.getStringFromFormData(clueDataEntityExt.getData(), ClueEnum.CLUE_STATUS.getAttr()));
                                                staticForMarketActivityEntity1.setClueStage(FastJsonHelper.getStringFromFormData(clueDataEntityExt.getData(), ClueEnum.CLUE_STAGE.getAttr()));
                                                staticForMarketActivityEntity1.setClueAddTime(clueDataEntityExt.getAddTime());
                                                staticForMarketActivityEntity1.setSaleOpportunityId(opportunity.getDataId());
                                                staticForMarketActivityEntity1.setSaleMoney(FastJsonHelper.getDoubleFromFormData(opportunity.getData(), SalesOpportunityEnum.ESTIMATE_AMOUNT.getAttr()));
                                                staticForMarketActivityEntity1.setSaleStage(FastJsonHelper.getStringFromFormData(opportunity.getData(), SalesOpportunityEnum.SALE_STAGE.getAttr()));
                                                insertListEntitys.add(staticForMarketActivityEntity1);
                                            }
                                            LOG.info("需要插入的數據： " + insertListEntitys.size());
                                        }
                                    } else {
                                        insertListEntitys.add(staticForMarketActivityEntity);
                                    }
                                }
                            }
                            //处理没有机会的客户数据
                            LOG.info("没有机会的客户数据 看看此时要移除的客户id     customerIdAll:" + customerIdAll.toString());
                            customerIdAll.forEach(customerId -> {
                                customerMap.remove(customerId);
                            });
                            customerMap.forEach((customerId, customerEntity) -> {
                                List<Long> customerClueList = customerClueMap.get(customerId);
                                LOG.info("没有销售机会的客户插入数据 看看此时的客户customerClueList:" + customerClueList.toString());
                                LOG.info("没有销售机会的客户插入数据 看看此时clueMap的值:" + clueMap.keySet().toString());
                                for (Long clueId : customerClueList) {
                                    clueIdAll.add(clueId);
                                    PaasFormDataEntityExt clueDataEntityExt = clueMap.get(clueId);
                                    if (Objects.nonNull(clueDataEntityExt)) {
                                        try {
                                            StaticForMarketActivityEntity staticForMarketActivityEntity = setEntityData(corpid, marketActivity, now);
                                            staticForMarketActivityEntity.setCustomerId(customerId);
                                            staticForMarketActivityEntity.setClueId(clueId);
                                            staticForMarketActivityEntity.setClueStatus(FastJsonHelper.getStringFromFormData(clueDataEntityExt.getData(), ClueEnum.CLUE_STATUS.getAttr()));
                                            staticForMarketActivityEntity.setClueStage(FastJsonHelper.getStringFromFormData(clueDataEntityExt.getData(), ClueEnum.CLUE_STAGE.getAttr()));
                                            staticForMarketActivityEntity.setClueAddTime(clueDataEntityExt.getAddTime());
                                            insertListEntitys.add(staticForMarketActivityEntity);
                                        } catch (XbbException e) {

                                        }

                                    }
                                    LOG.info("需要插入的數據： " + insertListEntitys.size());
                                }
                            });
                        }
                        LOG.info("没有客户的线索数据 看看此时要移除的线索id     clueIdAll:" + clueIdAll.toString());
                        clueIdAll.forEach(clueId -> {
                            clueMap.remove(clueId);
                        });
                        //处理没有客户的线索数据
                        // LOG.info("没有销售机会的客户插入数据 看看此时的客户customerClueList:" +customerClueList.toString());
                        LOG.info("没有销售机会的客户插入数据 看看此时clueMap的值:" + clueMap.keySet().toString());
                        clueMap.forEach((clueId, clueDataEntityExt) -> {
                            try {
                                StaticForMarketActivityEntity staticForMarketActivityEntity = setEntityData(corpid, marketActivity, now);
                                staticForMarketActivityEntity.setClueId(clueId);
                                staticForMarketActivityEntity.setClueStatus(FastJsonHelper.getStringFromFormData(clueDataEntityExt.getData(), ClueEnum.CLUE_STATUS.getAttr()));
                                staticForMarketActivityEntity.setClueStage(FastJsonHelper.getStringFromFormData(clueDataEntityExt.getData(), ClueEnum.CLUE_STATUS.getAttr()));
                                staticForMarketActivityEntity.setClueAddTime(clueDataEntityExt.getAddTime());
                                insertListEntitys.add(staticForMarketActivityEntity);
                            } catch (XbbException e) {

                            }
                            LOG.info("需要插入的數據： " + insertListEntitys.size());
                        });
                        if (CollectionsUtil.isNotEmpty(insertListEntitys)) {
                            List<Long> staticForMarketActivityIds = new ArrayList<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                            try {
//                                marketActivityForStaticModel.updateByMarketIdAndDel(staticForMarketActivityIds,corpid,marketActivity.getDataId(),3);
                                if (!hasDeleteMarketIds.contains(marketActivity.getDataId())) {
                                    marketActivityForStaticModel.deleteByMarketId(staticForMarketActivityIds, corpid, marketActivity.getDataId());
                                    hasDeleteMarketIds.add(marketActivity.getDataId());
                                }
                                marketActivityForStaticModel.insertBatch(insertListEntitys, corpid);
                            } catch (Exception e) {
                                LOG.error("统计数据出错，数据回滚", e);
//                                marketActivityForStaticModel.updateByMarketIdAndDel(staticForMarketActivityIds,corpid,marketActivity.getDataId(),0);
                            }

                        }
                    }
                }
            }
        }
        LOG.info("========================= 统计市场活动数据入库 定时任务执行结束时间：" + DateUtil.getInt() + " ==========================");
        LOG.info("------------------------- 统计市场活动数据入库 -> 定时任务 顺利结束 --------------------------");
        return null;
    }

    @Override
    public BaseVO syncContract(MarketActivity4StatisticsSyncContractDTO syncContractDTO) throws XbbException {
        log.info(LOG_FLAG, "统计市场活动同步合同 开始执行");
        long start = DateTimeUtil.getInt();
        log.info(LOG_FLAG, "执行开始时间：" + start);
        if (!proBaseConfig.getXbbCode().equals(syncContractDTO.getXbbCode())) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
        }
        List<String> corpidList = syncContractDTO.getCorpIdList();
        Integer startTime = syncContractDTO.getStartTime();
        Integer endTime = syncContractDTO.getEndTime();
        if (CollectionsUtil.isEmpty(corpidList)) {
            // fix 该方法中获取公司可用sql：EXPLAIN SELECT DISTINCT corpid from tb_statistics_for_market_activity
//            corpidList = companyModel.getCorpidList();
            corpidList = marketActivityModel.listAllCorpid();
        }
        log.info("共有" + corpidList.size() + "家数据");
        List<String> errorCorpidList = new ArrayList<>();
        Integer i = 0;
        Integer totalfail = 0;
        for (String corpid : corpidList) {
            i++;
            log.info("现在是第" + i + "家公司");
            try {
                // fix: 2022/3/16 主要处理逻辑在这
                syncContract(corpid, startTime, endTime);
            } catch (XbbException e) {
                totalfail++;
                log.error("同步公司" + corpid + "出现异常", e);
                errorCorpidList.add(corpid);
            }
        }
        log.info(LOG_FLAG, "统计市场活动同步合同 执行完毕");
        LOG.warn("同步花费时间:" + (DateTimeUtil.getInt() - start));
        LOG.warn("操作成功,本次共同步" + corpidList.size() + "家公司");
        LOG.warn("同步失败" + totalfail + "家公司");
        LOG.error("更新失败公司ID集合" + errorCorpidList.toString());
        return null;
    }

    @Override
    public BaseVO recalculate(MarketActivity4StatisticsRecalculateDTO recalculateDTO) throws XbbException {
        long now = DateTimeUtil.getInt();
        List<String> corpidList = recalculateDTO.getCorpIdList();
        List<Long> marketIds = recalculateDTO.getMarketIds();
        // 重新计算前置log打印
        recalculatePreLogPrint(now);
        // 重新计算前置校验 校验不通过直接抛错
        recalculatePreCheck(recalculateDTO);
        for (String corpid : corpidList) {
            // 查询需要重新计算的市场活动
            List<PaasFormDataEntityExt> marketActivityList = getMarketActivityList(corpid, marketIds);
            List<Long> hasDeleteMarketIds = new ArrayList<>();
            if (CollectionsUtil.isNotEmpty(marketActivityList)) {
                //线索
                for (PaasFormDataEntityExt marketActivity : marketActivityList) {
                    // 组装条件
                    BoolQueryBuilder cluesBoolQueryBuilder = packeageCluesBoolQueryBuilder(corpid, marketActivity.getDataId());
                    long idGet = 0;
                    Boolean hasMore = true;
                    while (hasMore) {
                        // 查询线索数据
                        List<PaasFormDataEntityExt> clueList = getClueDataEntityList(cluesBoolQueryBuilder, idGet);
                        Integer count = clueList.size();
                        if (count < 1000) {
                            hasMore = false;
                        }
                        //如果市场活动下没有线索，就只要插入市场活动的数据就好
                        if (count <= 0) {
                            marketActivityForStaticModel.deleteByMarketId(null, corpid, marketActivity.getDataId());
                            marketActivityForStaticModel.insert(setEntityData(corpid, marketActivity, now));
                            continue;
                        }
                        idGet = clueList.get(count - 1).getDataId();
                        //客户
                        List<Long> clueIdAll = new ArrayList<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                        List<Long> clueIdList = new ArrayList<>(clueList.size());
                        Map<Long, PaasFormDataEntityExt> clueMap = new HashMap<>(clueList.size());
                        for (PaasFormDataEntityExt clue : clueList) {
                            clueIdList.add(clue.getDataId());
                            clueMap.put(clue.getDataId(), clue);
                        }
                        // 组装客户查询条件
                        BoolQueryBuilder customerBoolQueryBuilder = packeageCustomerBoolQueryBuilder(corpid, clueIdList);
                        long customerIdGet = 0;
                        Boolean customerHasMore = true;
                        List<StaticForMarketActivityEntity> insertListEntitys = new ArrayList<>();
                        while (customerHasMore) {
                            // 查询客户
                            List<PaasFormDataEntityExt> customerList = getCustomerDataEntityList(customerBoolQueryBuilder, customerIdGet);
                            // 关联了机会 或者 合同的客户id集合
                            List<Long> customerIdAll = new ArrayList<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                            Integer customerCount = customerList.size();
                            if (customerCount < 1000) {
                                customerHasMore = false;
                            }
                            if (CollectionsUtil.isEmpty(customerList)) {
                                // 新增无转化客户的线索
                                continue;
                            }
                            customerIdGet = customerList.get(customerCount - 1).getDataId();
                            //机会
                            List<Long> customerIdList = new ArrayList<>(customerList.size());
                            customerIdList.add(-1L);
                            Map<Long, List<Long>> customerClueMap = new HashMap<>(customerList.size());
                            Map<Long, PaasFormDataEntityExt> customerMap = new HashMap<>(customerList.size());
                            for (PaasFormDataEntityExt customerEntityExt : customerList) {
                                Long customerId = customerEntityExt.getDataId();
                                JSONObject customerData = customerEntityExt.getData();
                                JSONArray clueIdArray = FastJsonHelper.getJsonArrOrDefaultFromFormData(customerData, CustomerManagementEnum.CLUE_ID.getAttr(), new JSONArray());
                                customerIdList.add(customerId);
                                List<Long> customerClueList = new ArrayList<>(clueIdArray.size());
                                clueIdArray.forEach(clueIdObject -> {
                                    customerClueList.add(Long.valueOf(clueIdObject.toString()));
                                });
                                customerClueMap.put(customerId, customerClueList);
                                customerMap.put(customerId, customerEntityExt);
                            }
//                            ===================================处理客户下的机会开始====================================================
                            // 组装机会查询条件
                            BoolQueryBuilder opportunityBoolQueryBuilder = packeageOpportunityBoolQueryBuilder(corpid, customerIdList);
                            long opportunityIdGet = 0;
                            Boolean opportunityHasMore = true;
                            while (opportunityHasMore) {
                                // 获取机会数据
                                List<PaasFormDataEntityExt> opportunityList = getOpportunityDataEntityList(opportunityBoolQueryBuilder, opportunityIdGet);
                                Integer opportunityCount = opportunityList.size();
                                if (opportunityCount < 1000) {
                                    opportunityHasMore = false;
                                }
                                if (CollectionsUtil.isEmpty(opportunityList)) {
                                    // insertList 新增数据 机会的客户
                                    continue;
                                }
                                opportunityIdGet = opportunityList.get(opportunityCount - 1).getDataId();
                                //插入数据
                                for (PaasFormDataEntityExt opportunity : opportunityList) {
                                    Long opportunityCustomerId = FastJsonHelper.getLongFromFormData(opportunity.getData(), SalesOpportunityEnum.CUSTOMER_NAME.getAttr());
                                    customerIdAll.add(opportunityCustomerId);
                                    StaticForMarketActivityEntity staticForMarketActivityEntity = setEntityData(corpid, marketActivity, now);
                                    staticForMarketActivityEntity.setCustomerId(opportunityCustomerId);
                                    List<Long> customerClueList = customerClueMap.get(opportunityCustomerId);
                                    PaasFormDataEntityExt customerEntity = customerMap.get(opportunityCustomerId);
                                    if (Objects.nonNull(customerEntity)) {
                                        customerMap.remove(opportunityCustomerId);
                                    }
                                    if (CollectionsUtil.isNotEmpty(customerClueList)) {
                                        for (Long clueId : customerClueList) {
                                            clueIdAll.add(clueId);
                                            PaasFormDataEntityExt clueDataEntityExt = clueMap.get(clueId);
                                            if (Objects.nonNull(clueDataEntityExt)) {
                                                StaticForMarketActivityEntity staticForMarketActivityEntity1 = new StaticForMarketActivityEntity();
                                                BeanUtil.copyProperties(staticForMarketActivityEntity, staticForMarketActivityEntity1);
                                                staticForMarketActivityEntity1.setClueId(clueId);
                                                staticForMarketActivityEntity1.setClueStatus(FastJsonHelper.getStringFromFormData(clueDataEntityExt.getData(), ClueEnum.CLUE_STATUS.getAttr()));
                                                staticForMarketActivityEntity1.setClueStage(FastJsonHelper.getStringFromFormData(clueDataEntityExt.getData(), ClueEnum.CLUE_STAGE.getAttr()));
                                                staticForMarketActivityEntity1.setClueAddTime(clueDataEntityExt.getAddTime());
                                                staticForMarketActivityEntity1.setSaleOpportunityId(opportunity.getDataId());
                                                staticForMarketActivityEntity1.setSaleMoney(FastJsonHelper.getDoubleFromFormData(opportunity.getData(), SalesOpportunityEnum.ESTIMATE_AMOUNT.getAttr()));
                                                staticForMarketActivityEntity1.setSaleStage(FastJsonHelper.getStringFromFormData(opportunity.getData(), SalesOpportunityEnum.SALE_STAGE.getAttr()));
                                                insertListEntitys.add(staticForMarketActivityEntity1);
                                            }
                                        }
                                    } else {
                                        insertListEntitys.add(staticForMarketActivityEntity);
                                    }
                                }
                            }
//                            ===================================处理客户下的机会结束====================================================

//                            ===================================处理客户下的合同开始====================================================
                            // 组装机会查询条件
                            BoolQueryBuilder contractBoolQueryBuilder = packeageContractBoolQueryBuilder(corpid, customerIdList);
                            long contractIdGet = 0;
                            Boolean contractHasMore = true;
                            while (contractHasMore) {
                                // 获取机会数据
                                List<PaasFormDataEntityExt> contractList = getContractDataEntityList(contractBoolQueryBuilder, contractIdGet);
                                Integer contractCount = contractList.size();
                                if (contractCount < 1000) {
                                    contractHasMore = false;
                                }
                                if (CollectionsUtil.isEmpty(contractList)) {
                                    // insertList 新增数据 机会的客户
                                    continue;
                                }
                                contractIdGet = contractList.get(contractCount - 1).getDataId();
                                //插入数据
                                for (PaasFormDataEntityExt contract : contractList) {
                                    Long customerId = FastJsonHelper.getLongFromFormData(contract.getData(), ContractEnum.LINK_CUSTOMER.getAttr());
                                    customerIdAll.add(customerId);
                                    StaticForMarketActivityEntity staticForMarketActivityEntity = setEntityData(corpid, marketActivity, now);
                                    staticForMarketActivityEntity.setCustomerId(customerId);
                                    List<Long> customerClueList = customerClueMap.get(customerId);
                                    PaasFormDataEntityExt customerEntity = customerMap.get(customerId);
                                    if (Objects.nonNull(customerEntity)) {
                                        customerMap.remove(customerId);
                                    }
                                    if (CollectionsUtil.isNotEmpty(customerClueList)) {
                                        for (Long clueId : customerClueList) {
                                            clueIdAll.add(clueId);
                                            PaasFormDataEntityExt clueDataEntityExt = clueMap.get(clueId);
                                            if (Objects.nonNull(clueDataEntityExt)) {
                                                StaticForMarketActivityEntity temp = new StaticForMarketActivityEntity();
                                                BeanUtil.copyProperties(staticForMarketActivityEntity, temp);
                                                temp.setClueId(clueId);
                                                temp.setClueStatus(FastJsonHelper.getStringFromFormData(clueDataEntityExt.getData(), ClueEnum.CLUE_STATUS.getAttr()));
                                                temp.setClueStage(FastJsonHelper.getStringFromFormData(clueDataEntityExt.getData(), ClueEnum.CLUE_STAGE.getAttr()));
                                                temp.setClueAddTime(clueDataEntityExt.getAddTime());
                                                temp.setContractId(contract.getId());
                                                temp.setContractMoney(FastJsonHelper.getDoubleFromFormData(contract.getData(), ContractEnum.AMOUNT.getAttr()));
                                                insertListEntitys.add(temp);
                                            }
                                        }
                                    } else {
                                        insertListEntitys.add(staticForMarketActivityEntity);
                                    }
                                }
                            }
//                            ===================================处理客户下的合同结束====================================================


                            //处理没有机会的客户数据
                            customerIdAll.forEach(customerId -> {
                                // 从所有的客户里移除有机会或者合同的
                                customerMap.remove(customerId);
                            });
                            customerMap.forEach((customerId, customerEntity) -> {
                                List<Long> customerClueList = customerClueMap.get(customerId);
                                for (Long clueId : customerClueList) {
                                    clueIdAll.add(clueId);
                                    PaasFormDataEntityExt clueDataEntityExt = clueMap.get(clueId);
                                    if (Objects.nonNull(clueDataEntityExt)) {
                                        try {
                                            StaticForMarketActivityEntity staticForMarketActivityEntity = setEntityData(corpid, marketActivity, now);
                                            staticForMarketActivityEntity.setCustomerId(customerId);
                                            staticForMarketActivityEntity.setClueId(clueId);
                                            staticForMarketActivityEntity.setClueStatus(FastJsonHelper.getStringFromFormData(clueDataEntityExt.getData(), ClueEnum.CLUE_STATUS.getAttr()));
                                            staticForMarketActivityEntity.setClueStage(FastJsonHelper.getStringFromFormData(clueDataEntityExt.getData(), ClueEnum.CLUE_STAGE.getAttr()));
                                            staticForMarketActivityEntity.setClueAddTime(clueDataEntityExt.getAddTime());
                                            insertListEntitys.add(staticForMarketActivityEntity);
                                        } catch (XbbException e) {

                                        }

                                    }
                                }
                            });


                        }
                        clueIdAll.forEach(clueId -> {
                            // 所有的线索 移除有客户的线索
                            clueMap.remove(clueId);
                        });
                        //处理没有客户的线索数据
                        // LOG.info("没有销售机会的客户插入数据 看看此时的客户customerClueList:" +customerClueList.toString());
                        clueMap.forEach((clueId, clueDataEntityExt) -> {
                            try {
                                StaticForMarketActivityEntity staticForMarketActivityEntity = setEntityData(corpid, marketActivity, now);
                                staticForMarketActivityEntity.setClueId(clueId);
                                staticForMarketActivityEntity.setClueStatus(FastJsonHelper.getStringFromFormData(clueDataEntityExt.getData(), ClueEnum.CLUE_STATUS.getAttr()));
                                staticForMarketActivityEntity.setClueStage(FastJsonHelper.getStringFromFormData(clueDataEntityExt.getData(), ClueEnum.CLUE_STATUS.getAttr()));
                                staticForMarketActivityEntity.setClueAddTime(clueDataEntityExt.getAddTime());
                                insertListEntitys.add(staticForMarketActivityEntity);
                            } catch (XbbException e) {

                            }
                        });
                        if (CollectionsUtil.isNotEmpty(insertListEntitys)) {
                            List<Long> staticForMarketActivityIds = new ArrayList<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                            try {
//                                marketActivityForStaticModel.updateByMarketIdAndDel(staticForMarketActivityIds,corpid,marketActivity.getDataId(),3);
                                if (!hasDeleteMarketIds.contains(marketActivity.getDataId())) {
                                    marketActivityForStaticModel.deleteByMarketId(staticForMarketActivityIds, corpid, marketActivity.getDataId());
                                    hasDeleteMarketIds.add(marketActivity.getDataId());
                                }
                                marketActivityForStaticModel.insertBatch(insertListEntitys, corpid);
                            } catch (Exception e) {
                                LOG.error("统计数据出错", e);
//                                marketActivityForStaticModel.updateByMarketIdAndDel(staticForMarketActivityIds,corpid,marketActivity.getDataId(),0);
                            }

                        }
                    }
                }
            }
        }
        // 打印结束日志
        recalculateSuffixLogPrint();
        return null;
    }

    /**
     * 重新计算逻辑包括回收站的
     * date: 2022/3/24 5:20 下午
     *
     * @param recalculateDTO:
     * @author yingjf
     */
    @Override
    public BaseVO recalculateIncludeRecycle(MarketActivity4StatisticsRecalculateDTO recalculateDTO) throws XbbException {
        long now = DateTimeUtil.getInt();
        List<String> corpidList = recalculateDTO.getCorpIdList();
        List<Long> marketIds = recalculateDTO.getMarketIds();
        // 重新计算前置log打印
        recalculatePreLogPrint(now);
        // 重新计算前置校验 校验不通过直接抛错
        recalculatePreCheck(recalculateDTO);
        for (String corpid : corpidList) {
            // 查询需要重新计算的市场活动
            List<PaasFormDataEntityExt> marketActivityList = getMarketActivityList(corpid, marketIds);
            List<Long> hasDeleteMarketIds = new ArrayList<>();
            if (CollectionsUtil.isNotEmpty(marketActivityList)) {
                //线索
                for (PaasFormDataEntityExt marketActivity : marketActivityList) {
                    // 组装条件   查出包括回收站的线索
                    BoolQueryBuilder cluesBoolQueryBuilder = packeageCluesBoolQueryBuilderIncludeRecycle(corpid, marketActivity.getDataId());
                    long idGet = 0;
                    Boolean hasMore = true;
                    while (hasMore) {
                        // 查询线索数据    查出包括回收站的线索
                        List<PaasFormDataEntityExt> clueList = getClueDataEntityList(cluesBoolQueryBuilder, idGet);
                        Integer count = clueList.size();
                        if(CollectionsUtil.isNotEmpty(clueList)){
                            idGet = clueList.get(count - 1).getDataId();
                        }
                        if (count < 1000) {
                            hasMore = false;
                        }
                        List<Long> clueListNoRecycle = new ArrayList<>();
                        List<Long> clueListisRecycle = new ArrayList<>();
                        //客户  有客户的线索
                        List<Long> clueIdAll = new ArrayList<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                        Map<Long, PaasFormDataEntityExt> clueMapNoRecycle = new HashMap<>(clueList.size());
                        Map<Long, PaasFormDataEntityExt> clueMapisRecycle = new HashMap<>(clueList.size());
                        Map<Long, PaasFormDataEntityExt> clueMapisRecycleBase = new HashMap<>(clueList.size());
                        for (PaasFormDataEntityExt temp : clueList) {
                            Integer del = temp.getDel();
                            if (Objects.equals(del, DelEnum.NORMAL.getDel())) {
                                clueListNoRecycle.add(temp.getDataId());
                                clueMapNoRecycle.put(temp.getDataId(), temp);
                            } else {
                                clueListisRecycle.add(temp.getDataId());
                                clueMapisRecycle.put(temp.getDataId(), temp);
                                clueMapisRecycleBase.put(temp.getDataId(), temp);
                            }
                        }
                        //如果市场活动下没有线索，就只要插入市场活动的数据就好
                        if (clueListNoRecycle.size() <= 0) {
                            marketActivityForStaticModel.deleteByMarketId(null, corpid, marketActivity.getDataId());
                            StaticForMarketActivityEntity staticForMarketActivityEntity = setEntityData(corpid, marketActivity, now);
                            marketActivityForStaticModel.insert(staticForMarketActivityEntity);
                            hasDeleteMarketIds.add(marketActivity.getDataId());
                        }
                        // 回收站的
                        List<StaticForMarketActivityEntity> insertListEntitys = new ArrayList<>();
                        if (CollectionsUtil.isNotEmpty(clueListisRecycle)) {
                            recalculateClueIncludeRecycle(corpid, marketActivity, now,
                                    clueIdAll, clueListisRecycle, clueMapisRecycle, insertListEntitys, 1);
                        }
                        // 非回收站的
                        if (CollectionsUtil.isNotEmpty(clueListNoRecycle)) {
                            recalculateClueIncludeRecycle(corpid, marketActivity, now,
                                    clueIdAll, clueListNoRecycle, clueMapNoRecycle, insertListEntitys, 0);
                        }
                        // 这里有个补偿操作  如果全是回收站的客户 则需要补偿线索
                        if (CollectionsUtil.isNotEmpty(clueListisRecycle) && CollectionsUtil.isEmpty(clueListNoRecycle)) {
                            StaticForMarketActivityEntity staticForMarketActivityEntity = null;
                            for (Long clueId : clueListisRecycle) {
                                PaasFormDataEntityExt clueDataEntityExt = clueMapisRecycleBase.get(clueId);
                                staticForMarketActivityEntity = setEntityData(corpid, marketActivity, now);
                                staticForMarketActivityEntity.setClueId(clueId);
                                staticForMarketActivityEntity.setClueStatus(FastJsonHelper.getStringFromFormData(clueDataEntityExt.getData(), ClueEnum.CLUE_STATUS.getAttr()));
                                staticForMarketActivityEntity.setClueStage(FastJsonHelper.getStringFromFormData(clueDataEntityExt.getData(), ClueEnum.CLUE_STAGE.getAttr()));
                                staticForMarketActivityEntity.setClueAddTime(clueDataEntityExt.getAddTime());
                                staticForMarketActivityEntity.setDel(1);
                            }
                        }

                        if (CollectionsUtil.isNotEmpty(insertListEntitys)) {
                            List<Long> staticForMarketActivityIds = new ArrayList<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                            try {
                                if (!hasDeleteMarketIds.contains(marketActivity.getDataId())) {
                                    marketActivityForStaticModel.deleteByMarketId(staticForMarketActivityIds, corpid, marketActivity.getDataId());
                                    hasDeleteMarketIds.add(marketActivity.getDataId());
                                }
                                marketActivityForStaticModel.insertBatch(insertListEntitys, corpid);
                            } catch (Exception e) {
                                LOG.error("统计数据出错", e);
                            }

                        }
                    }
                }
            }
        }
        // 打印结束日志
        recalculateSuffixLogPrint();
        return null;
    }

    /**
     * 处理线索
     * date: 2022/3/24 5:20 下午
     *
     * @param corpid:            公司id
     * @param marketActivity:    市场活动数据
     * @param now:               时间
     * @param clueIdAll:
     * @param clueIdList:        线索集合
     * @param clueMap:           key为线索 value为线索数据实体
     * @param insertListEntitys: 需要插入中间表的数据集合
     * @param clueDel:           删除标志
     * @author yingjf
     */
    private void recalculateClueIncludeRecycle(String corpid, PaasFormDataEntityExt marketActivity, long now,
                                               List<Long> clueIdAll, List<Long> clueIdList, Map<Long, PaasFormDataEntityExt> clueMap
            , List<StaticForMarketActivityEntity> insertListEntitys, Integer clueDel) throws XbbException {
        // 组装客户查询条件  查出包括回收站的
        BoolQueryBuilder customerBoolQueryBuilder = packeageCustomerBoolQueryBuilderIncludeRecycle(corpid, clueIdList);
        long customerIdGet = 0;
        Boolean customerHasMore = true;
        while (customerHasMore) {
            // 查询客户
            List<PaasFormDataEntityExt> customerList = getCustomerDataEntityList(customerBoolQueryBuilder, customerIdGet);
            // 关联了机会 或者 合同的客户id集合
            List<Long> customerIdAll = new ArrayList<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);

            Integer customerCount = customerList.size();
            if (customerCount < 1000) {
                customerHasMore = false;
            }
            if (CollectionsUtil.isEmpty(customerList)) {
                // 新增无转化客户的线索
                continue;
            }
            if(CollectionsUtil.isNotEmpty(customerList)){
                customerIdGet = customerList.get(customerCount - 1).getDataId();
            }
            List<PaasFormDataEntityExt> customerNoRecycle = new ArrayList<>();
            List<PaasFormDataEntityExt> customerIsRecycle = new ArrayList<>();
            //机会
            List<Long> customerIdList = new ArrayList<>(customerList.size());
            customerIdList.add(-1L);
            Map<Long, List<Long>> customerClueMap = new HashMap<>(customerList.size());
            Map<Long, PaasFormDataEntityExt> customerMap = new HashMap<>(customerList.size());
            for (PaasFormDataEntityExt temp : customerList) {
                Integer del = temp.getDel();
                if (Objects.equals(del, DelEnum.NORMAL.getDel())) {
                    customerNoRecycle.add(temp);
                } else {
                    customerIsRecycle.add(temp);
                }
                Long customerId = temp.getDataId();
                JSONObject customerData = temp.getData();
                JSONArray clueIdArray = FastJsonHelper.getJsonArrOrDefaultFromFormData(customerData, CustomerManagementEnum.CLUE_ID.getAttr(), new JSONArray());
                customerIdList.add(customerId);
                List<Long> customerClueList = new ArrayList<>(clueIdArray.size());
                clueIdArray.forEach(clueIdObject -> {
                    customerClueList.add(Long.valueOf(clueIdObject.toString()));
                });
                customerClueMap.put(customerId, customerClueList);
                customerMap.put(customerId, temp);
            }
            // 处理非回收站的客户数据 customerNoRecycle
            if (CollectionsUtil.isNotEmpty(customerNoRecycle)) {
                recalculateCustomerIncludeRecycle(corpid, marketActivity,
                        now, insertListEntitys,
                        clueIdAll, clueMap, customerIdAll,
                        customerNoRecycle, clueDel);
            }
            // 处理回收站的客户数据 customerIsRecycle
            if (CollectionsUtil.isNotEmpty(customerIsRecycle)) {
                recalculateCustomerIncludeRecycle(corpid, marketActivity,
                        now, insertListEntitys,
                        clueIdAll, clueMap, customerIdAll,
                        customerIsRecycle, clueDel);
            }
            // 这里有个补偿操作  如果全是回收站的客户 则需要补偿线索
            if (CollectionsUtil.isNotEmpty(customerIsRecycle) && CollectionsUtil.isEmpty(customerNoRecycle)) {
                for (PaasFormDataEntityExt customerEntityExt : customerIsRecycle) {
                    Long customerId = customerEntityExt.getDataId();
                    JSONObject customerData = customerEntityExt.getData();
                    JSONArray clueIdArray = FastJsonHelper.getJsonArrOrDefaultFromFormData(customerData, CustomerManagementEnum.CLUE_ID.getAttr(), new JSONArray());
                    customerIdList.add(customerId);
                    List<Long> customerClueList = new ArrayList<>(clueIdArray.size());
                    clueIdArray.forEach(clueIdObject -> {
                        customerClueList.add(Long.valueOf(clueIdObject.toString()));
                    });
                    Long clueId = customerClueList.get(0);
                    if (Objects.isNull(clueId)) {
                        continue;
                    }
                    PaasFormDataEntityExt clueDataEntityExt = clueMap.get(clueId);
                    if (Objects.isNull(clueDataEntityExt)) {
                        continue;
                    }
                    StaticForMarketActivityEntity temp = setEntityData(corpid, marketActivity, now);
                    temp.setClueId(clueId);
                    temp.setClueStatus(FastJsonHelper.getStringFromFormData(clueDataEntityExt.getData(), ClueEnum.CLUE_STATUS.getAttr()));
                    temp.setClueStage(FastJsonHelper.getStringFromFormData(clueDataEntityExt.getData(), ClueEnum.CLUE_STAGE.getAttr()));
                    temp.setClueAddTime(clueDataEntityExt.getAddTime());
                    temp.setCustomerId(customerId);
                    temp.setDel(0);
                    insertListEntitys.add(temp);
                }
            }

        }

        clueIdAll.forEach(clueId -> {
            // 所有的线索 移除有客户的线索
            clueMap.remove(clueId);
        });

        clueMap.forEach((clueId, clueDataEntityExt) -> {
            try {
                clueIdAll.add(clueId);
                StaticForMarketActivityEntity staticForMarketActivityEntity = setEntityData(corpid, marketActivity, now);
                staticForMarketActivityEntity.setClueId(clueId);
                staticForMarketActivityEntity.setClueStatus(FastJsonHelper.getStringFromFormData(clueDataEntityExt.getData(), ClueEnum.CLUE_STATUS.getAttr()));
                staticForMarketActivityEntity.setClueStage(FastJsonHelper.getStringFromFormData(clueDataEntityExt.getData(), ClueEnum.CLUE_STAGE.getAttr()));
                staticForMarketActivityEntity.setClueAddTime(clueDataEntityExt.getAddTime());
                staticForMarketActivityEntity.setDel(clueDel);
                insertListEntitys.add(staticForMarketActivityEntity);
            } catch (XbbException e) {

            }
        });
    }

    /**
     * date: 2022/3/25 9:55 上午
     *
     * @param corpid:            公司id
     * @param marketActivity:    市场活动
     * @param now:               时间
     * @param insertListEntitys: 需要插入中间表的数据集合
     * @param clueIdAll:
     * @param clueMap:           key为线索 value为线索数据实体
     * @param customerIdAll:     客户id集合
     * @param customerList:      客户集合
     * @param del:               删除标志
     * @Author yingjf
     */
    private void recalculateCustomerIncludeRecycle(String corpid, PaasFormDataEntityExt marketActivity,
                                                   long now, List<StaticForMarketActivityEntity> insertListEntitys,
                                                   List<Long> clueIdAll, Map<Long, PaasFormDataEntityExt> clueMap, List<Long> customerIdAll,
                                                   List<PaasFormDataEntityExt> customerList, Integer del) throws XbbException {
        //机会
        List<Long> customerIdList = new ArrayList<>(customerList.size());
        customerIdList.add(-1L);
        // key为客户 value为线索集合
        Map<Long, List<Long>> customerClueMap = new HashMap<>(customerList.size());
        Map<Long, PaasFormDataEntityExt> customerMap = new HashMap<>(customerList.size());
        for (PaasFormDataEntityExt customerEntityExt : customerList) {
            Long customerId = customerEntityExt.getDataId();
            JSONObject customerData = customerEntityExt.getData();
            JSONArray clueIdArray = FastJsonHelper.getJsonArrOrDefaultFromFormData(customerData, CustomerManagementEnum.CLUE_ID.getAttr(), new JSONArray());
            customerIdList.add(customerId);
            List<Long> customerClueList = new ArrayList<>(clueIdArray.size());
            clueIdArray.forEach(clueIdObject -> {
                customerClueList.add(Long.valueOf(clueIdObject.toString()));
            });
            customerClueMap.put(customerId, customerClueList);
            customerMap.put(customerId, customerEntityExt);
        }
//                            ===================================处理客户下的机会开始====================================================
        // 组装机会查询条件
        BoolQueryBuilder opportunityBoolQueryBuilder = packeageOpportunityBoolQueryBuilder(corpid, customerIdList);
        long opportunityIdGet = 0;
        Boolean opportunityHasMore = true;
        while (opportunityHasMore) {
            // 获取机会数据
            List<PaasFormDataEntityExt> opportunityList = getOpportunityDataEntityList(opportunityBoolQueryBuilder, opportunityIdGet);
            Integer opportunityCount = opportunityList.size();
            if (opportunityCount < 1000) {
                opportunityHasMore = false;
            }
            if (CollectionsUtil.isEmpty(opportunityList)) {
                // insertList 新增数据 机会的客户
                continue;
            }
            if(CollectionsUtil.isNotEmpty(opportunityList)){
                opportunityIdGet = opportunityList.get(opportunityCount - 1).getDataId();
            }
            //插入数据
            for (PaasFormDataEntityExt opportunity : opportunityList) {
                Long opportunityCustomerId = FastJsonHelper.getLongFromFormData(opportunity.getData(), SalesOpportunityEnum.CUSTOMER_NAME.getAttr());
                customerIdAll.add(opportunityCustomerId);
                StaticForMarketActivityEntity staticForMarketActivityEntity = setEntityData(corpid, marketActivity, now);
                staticForMarketActivityEntity.setCustomerId(opportunityCustomerId);
                List<Long> customerClueList = customerClueMap.get(opportunityCustomerId);
                PaasFormDataEntityExt customerEntity = customerMap.get(opportunityCustomerId);
                if (Objects.nonNull(customerEntity)) {
                    customerMap.remove(opportunityCustomerId);
                }
                if (CollectionsUtil.isNotEmpty(customerClueList)) {
                    for (Long clueId : customerClueList) {
                        clueIdAll.add(clueId);
                        PaasFormDataEntityExt clueDataEntityExt = clueMap.get(clueId);
                        if (Objects.nonNull(clueDataEntityExt)) {
                            StaticForMarketActivityEntity staticForMarketActivityEntity1 = new StaticForMarketActivityEntity();
                            BeanUtil.copyProperties(staticForMarketActivityEntity, staticForMarketActivityEntity1);
                            staticForMarketActivityEntity1.setClueId(clueId);
                            staticForMarketActivityEntity1.setClueStatus(FastJsonHelper.getStringFromFormData(clueDataEntityExt.getData(), ClueEnum.CLUE_STATUS.getAttr()));
                            staticForMarketActivityEntity1.setClueStage(FastJsonHelper.getStringFromFormData(clueDataEntityExt.getData(), ClueEnum.CLUE_STAGE.getAttr()));
                            staticForMarketActivityEntity1.setClueAddTime(clueDataEntityExt.getAddTime());
                            staticForMarketActivityEntity1.setSaleOpportunityId(opportunity.getDataId());
                            staticForMarketActivityEntity1.setSaleMoney(FastJsonHelper.getDoubleFromFormData(opportunity.getData(), SalesOpportunityEnum.ESTIMATE_AMOUNT.getAttr()));
                            staticForMarketActivityEntity1.setSaleStage(FastJsonHelper.getStringFromFormData(opportunity.getData(), SalesOpportunityEnum.SALE_STAGE.getAttr()));
                            staticForMarketActivityEntity1.setDel(del);
                            insertListEntitys.add(staticForMarketActivityEntity1);
                        }
                    }
                } else {
                    insertListEntitys.add(staticForMarketActivityEntity);
                }
            }
        }
//                            ===================================处理客户下的机会结束====================================================
//                            ===================================处理客户下的合同开始====================================================
        // 组装机会查询条件
        BoolQueryBuilder contractBoolQueryBuilder = packeageContractBoolQueryBuilder(corpid, customerIdList);
        long contractIdGet = 0;
        Boolean contractHasMore = true;
        while (contractHasMore) {
            // 获取机会数据
            List<PaasFormDataEntityExt> contractList = getContractDataEntityList(contractBoolQueryBuilder, contractIdGet);
            Integer contractCount = contractList.size();
            if (contractCount < 1000) {
                contractHasMore = false;
            }
            if (CollectionsUtil.isEmpty(contractList)) {
                // insertList 新增数据 机会的客户
                continue;
            }
            if(CollectionsUtil.isNotEmpty(contractList)){
                contractIdGet = contractList.get(contractCount - 1).getDataId();
            }
            //插入数据
            for (PaasFormDataEntityExt contract : contractList) {
                Long customerId = FastJsonHelper.getLongFromFormData(contract.getData(), ContractEnum.LINK_CUSTOMER.getAttr());
                customerIdAll.add(customerId);
                StaticForMarketActivityEntity staticForMarketActivityEntity = setEntityData(corpid, marketActivity, now);
                staticForMarketActivityEntity.setCustomerId(customerId);
                List<Long> customerClueList = customerClueMap.get(customerId);
                PaasFormDataEntityExt customerEntity = customerMap.get(customerId);
                if (Objects.nonNull(customerEntity)) {
                    customerMap.remove(customerId);
                }
                if (CollectionsUtil.isNotEmpty(customerClueList)) {
                    for (Long clueId : customerClueList) {
                        clueIdAll.add(clueId);
                        PaasFormDataEntityExt clueDataEntityExt = clueMap.get(clueId);
                        if (Objects.nonNull(clueDataEntityExt)) {
                            StaticForMarketActivityEntity temp = new StaticForMarketActivityEntity();
                            BeanUtil.copyProperties(staticForMarketActivityEntity, temp);
                            temp.setClueId(clueId);
                            temp.setClueStatus(FastJsonHelper.getStringFromFormData(clueDataEntityExt.getData(), ClueEnum.CLUE_STATUS.getAttr()));
                            temp.setClueStage(FastJsonHelper.getStringFromFormData(clueDataEntityExt.getData(), ClueEnum.CLUE_STAGE.getAttr()));
                            temp.setClueAddTime(clueDataEntityExt.getAddTime());
                            temp.setContractId(contract.getId());
                            temp.setContractMoney(FastJsonHelper.getDoubleFromFormData(contract.getData(), ContractEnum.AMOUNT.getAttr()));
                            temp.setDel(del);
                            insertListEntitys.add(temp);
                        }
                    }
                } else {
                    insertListEntitys.add(staticForMarketActivityEntity);
                }
            }
        }
//                            ===================================处理客户下的合同结束====================================================
        //处理没有机会的客户数据
        customerIdAll.forEach(customerId -> {
            // 从所有的客户里移除有机会或者合同的
            customerMap.remove(customerId);
        });
        customerMap.forEach((customerId, customerEntity) -> {
            List<Long> customerClueList = customerClueMap.get(customerId);
            for (Long clueId : customerClueList) {
                clueIdAll.add(clueId);
                PaasFormDataEntityExt clueDataEntityExt = clueMap.get(clueId);
                if (Objects.nonNull(clueDataEntityExt)) {
                    try {
                        StaticForMarketActivityEntity staticForMarketActivityEntity = setEntityData(corpid, marketActivity, now);
                        staticForMarketActivityEntity.setCustomerId(customerId);
                        staticForMarketActivityEntity.setClueId(clueId);
                        staticForMarketActivityEntity.setClueStatus(FastJsonHelper.getStringFromFormData(clueDataEntityExt.getData(), ClueEnum.CLUE_STATUS.getAttr()));
                        staticForMarketActivityEntity.setClueStage(FastJsonHelper.getStringFromFormData(clueDataEntityExt.getData(), ClueEnum.CLUE_STAGE.getAttr()));
                        staticForMarketActivityEntity.setClueAddTime(clueDataEntityExt.getAddTime());
                        staticForMarketActivityEntity.setDel(del);
                        insertListEntitys.add(staticForMarketActivityEntity);
                    } catch (XbbException e) {
                    }
                }
            }
        });
    }


    /**
     * 获取合同数据
     * date: 2022/3/16 2:48 下午
     *
     * @param contractBoolQueryBuilder:
     * @param contractIdGet:
     * @author yingjf
     */
    private List<PaasFormDataEntityExt> getContractDataEntityList(BoolQueryBuilder contractBoolQueryBuilder, long contractIdGet) throws XbbException {
        SearchSourceBuilder contractSourceBuilder = new SearchSourceBuilder();
        SearchRequest contractSearchRequest = new SearchRequest(IndexTypeEnum.IDX_SAAS_CONTRACT.getIndex());
        PageRequest contractPageRequest = null;
        int opportunityPage = 0;
        contractBoolQueryBuilder.filter(rangeQuery("dataId").gte(contractIdGet));
        contractSourceBuilder.query(contractBoolQueryBuilder);
        contractSourceBuilder.sort(new FieldSortBuilder("dataId").order(SortOrder.ASC));
        contractPageRequest = EsUtil.setPage(contractSourceBuilder, opportunityPage, 1000);
        contractSearchRequest.source(contractSourceBuilder);
        XbbAggregatedPage<PaasFormDataEntityExt> opportunityEntities = xbbElasticsearchRestTemplate.queryForPages(contractPageRequest, contractSearchRequest, PaasFormDataEntityExt.class);
        List<PaasFormDataEntityExt> opportunityList = opportunityEntities.getContent();
        return opportunityList;
    }

    /**
     * 获取机会数据
     * date: 2022/3/16 2:48 下午
     *
     * @param opportunityBoolQueryBuilder:
     * @param opportunityIdGet:
     * @author yingjf
     */
    private List<PaasFormDataEntityExt> getOpportunityDataEntityList(BoolQueryBuilder opportunityBoolQueryBuilder, long opportunityIdGet) throws XbbException {
        SearchSourceBuilder opportunitySourceBuilder = new SearchSourceBuilder();
        SearchRequest opportunitySearchRequest = new SearchRequest(IndexTypeEnum.IDX_SAAS_OPPORTUNITY.getIndex());
        PageRequest opportunityPageRequest = null;
        int opportunityPage = 0;
        opportunityBoolQueryBuilder.filter(rangeQuery("dataId").gte(opportunityIdGet));
        opportunitySourceBuilder.query(opportunityBoolQueryBuilder);
        opportunitySourceBuilder.sort(new FieldSortBuilder("dataId").order(SortOrder.ASC));
        opportunityPageRequest = EsUtil.setPage(opportunitySourceBuilder, opportunityPage, 1000);
        opportunitySearchRequest.source(opportunitySourceBuilder);
        XbbAggregatedPage<PaasFormDataEntityExt> opportunityEntities = xbbElasticsearchRestTemplate.queryForPages(opportunityPageRequest, opportunitySearchRequest, PaasFormDataEntityExt.class);
        List<PaasFormDataEntityExt> opportunityList = opportunityEntities.getContent();
        return opportunityList;
    }

    /**
     * 查询客户数据
     * date: 2022/3/16 2:46 下午
     *
     * @param customerBoolQueryBuilder:
     * @param customerIdGet:
     * @author yingjf
     */
    private List<PaasFormDataEntityExt> getCustomerDataEntityList(BoolQueryBuilder customerBoolQueryBuilder, long customerIdGet) throws XbbException {
        SearchSourceBuilder customerSourceBuilder = new SearchSourceBuilder();
        SearchRequest customerSearchRequest = new SearchRequest(IndexTypeEnum.IDX_SAAS_CUSTOMER.getIndex());
        PageRequest customerPageRequest = null;
        int customerPage = 0;
        customerBoolQueryBuilder.filter(rangeQuery("dataId").gte(customerIdGet));
        customerSourceBuilder.query(customerBoolQueryBuilder);
        customerSourceBuilder.sort(new FieldSortBuilder("dataId").order(SortOrder.ASC));
        customerPageRequest = EsUtil.setPage(customerSourceBuilder, customerPage, 1000);
        customerSearchRequest.source(customerSourceBuilder);
        XbbAggregatedPage<PaasFormDataEntityExt> customerEntities = xbbElasticsearchRestTemplate.queryForPages(customerPageRequest, customerSearchRequest, PaasFormDataEntityExt.class);
        List<PaasFormDataEntityExt> customerList = customerEntities.getContent();
        return customerList;
    }

    /**
     * 查询线索数据
     * date: 2022/3/16 2:32 下午
     *
     * @param cluesBoolQueryBuilder:
     * @author yingjf
     */
    private List<PaasFormDataEntityExt> getClueDataEntityList(BoolQueryBuilder cluesBoolQueryBuilder, long idGet) throws XbbException {
        PageRequest pageRequest = null;
        int page = 0;
        SearchRequest clueSearchRequest = new SearchRequest(IndexTypeEnum.IDX_SAAS_CLUE.getIndex());
        SearchSourceBuilder clueSourceBuilder = new SearchSourceBuilder();
        cluesBoolQueryBuilder.filter(rangeQuery("dataId").gte(idGet));
        clueSourceBuilder.query(cluesBoolQueryBuilder);
        clueSourceBuilder.sort(new FieldSortBuilder("dataId").order(SortOrder.ASC));
        pageRequest = EsUtil.setPage(clueSourceBuilder, page, 1000);
        clueSearchRequest.source(clueSourceBuilder);
        XbbAggregatedPage<PaasFormDataEntityExt> esEntities = xbbElasticsearchRestTemplate.queryForPages(pageRequest, clueSearchRequest, PaasFormDataEntityExt.class);
        List<PaasFormDataEntityExt> clueList = esEntities.getContent();
        return clueList;
    }

    /**
     * 重新统计脚本后置日志打印
     * date: 2022/3/16 2:20 下午
     *
     * @param :
     * @author yingjf
     */
    private void recalculateSuffixLogPrint() {
        log.info(LOG_FLAG, "统计市场活动-> 重新计算脚本 结束时间" + DateUtil.getInt());
        log.info(LOG_FLAG, "统计市场活动-> 重新计算脚本 顺利结束");
    }

    /**
     * 重新统计脚本前置日志打印
     * date: 2022/3/16 2:20 下午
     *
     * @param start:
     * @author yingjf
     */
    private void recalculatePreLogPrint(long start) {
        log.info(LOG_FLAG, "统计市场活动-> 重新计算脚本 开始执行");
        log.info(LOG_FLAG, "执行开始时间：" + start);
    }

    /**
     * 重新计算的前置校验
     * date: 2022/3/16 1:54 下午
     *
     * @param :
     * @author yingjf
     */
    private void recalculatePreCheck(MarketActivity4StatisticsRecalculateDTO marketActivity4StatisticsRecalculateDTO) throws XbbException {
        if (!proBaseConfig.getXbbCode().equals(marketActivity4StatisticsRecalculateDTO.getXbbCode())) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
        }
        List<String> corpidList = marketActivity4StatisticsRecalculateDTO.getCorpIdList();
        List<Long> marketIds = marketActivity4StatisticsRecalculateDTO.getMarketIds();
        if (corpidList.size() > 1 && marketIds.size() > 1) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
    }

    /**
     * 组装合同查询条件
     * date: 2022/3/16 4:34 下午
     *
     * @param corpid:
     * @param contractIdList:
     * @author yingjf
     */
    private BoolQueryBuilder packeageContractBoolQueryBuilder(String corpid, List<Long> contractIdList) throws XbbException {
        BoolQueryBuilder contractBoolQueryBuilder = boolQuery();
        contractBoolQueryBuilder.filter(termQuery("corpid.keyword", corpid));
        contractBoolQueryBuilder.filter(termQuery("del", 0));
        contractBoolQueryBuilder.filter(termsQuery(ContractEnum.getEsAttr4Keyword(ContractEnum.LINK_CUSTOMER), contractIdList));
        contractBoolQueryBuilder.filter(termQuery(ElasticConstant.MY_JOIN_FIELD, IndexTypeEnum.IDX_SAAS_CONTRACT.getType()));
        // 排除审批中的
        contractBoolQueryBuilder.filter(termsQuery("flowStatus", FlowStatusEnum.getStatisticType()));
        // 排除红冲合同
        contractBoolQueryBuilder.filter(termQuery(ContractEnum.getEsAttr4Keyword(ContractEnum.IS_RED), 0));
        return contractBoolQueryBuilder;
    }

    /**
     * 组装机会查询条件
     * date: 2022/3/16 1:52 下午
     *
     * @param corpid:
     * @param customerIdList:
     * @author yingjf
     */
    private BoolQueryBuilder packeageOpportunityBoolQueryBuilder(String corpid, List<Long> customerIdList) throws XbbException {
        BoolQueryBuilder opportunityBoolQueryBuilder = boolQuery();
        opportunityBoolQueryBuilder.filter(termQuery("corpid.keyword", corpid));
        opportunityBoolQueryBuilder.filter(termQuery("del", 0));
        opportunityBoolQueryBuilder.filter(termsQuery(SalesOpportunityEnum.getEsAttr4Keyword(SalesOpportunityEnum.CUSTOMER_NAME), customerIdList));
        opportunityBoolQueryBuilder.filter(termQuery(ElasticConstant.MY_JOIN_FIELD, IndexTypeEnum.IDX_SAAS_OPPORTUNITY.getType()));
        opportunityBoolQueryBuilder.filter(termsQuery("flowStatus", FlowStatusEnum.getStatisticType()));
        return opportunityBoolQueryBuilder;
    }

    /**
     * 组装客户查询条件
     * date: 2022/3/16 1:49 下午
     *
     * @param corpid:
     * @param clueIdList:
     * @author yingjf
     */
    private BoolQueryBuilder packeageCustomerBoolQueryBuilder(String corpid, List<Long> clueIdList) throws XbbException {
        BoolQueryBuilder customerBoolQueryBuilder = boolQuery();
        customerBoolQueryBuilder.filter(termQuery("corpid.keyword", corpid));
        customerBoolQueryBuilder.filter(termQuery("del", 0));
        customerBoolQueryBuilder.filter(termsQuery(CustomerManagementEnum.getEsAttr4Keyword(CustomerManagementEnum.CLUE_ID), clueIdList));
        customerBoolQueryBuilder.filter(termQuery(ElasticConstant.MY_JOIN_FIELD, IndexTypeEnum.IDX_SAAS_CUSTOMER.getType()));
        customerBoolQueryBuilder.filter(termsQuery("flowStatus", FlowStatusEnum.getStatisticType()));
        return customerBoolQueryBuilder;
    }

    private BoolQueryBuilder packeageCustomerBoolQueryBuilderIncludeRecycle(String corpid, List<Long> clueIdList) throws XbbException {
        BoolQueryBuilder customerBoolQueryBuilder = boolQuery();
        customerBoolQueryBuilder.filter(termQuery("corpid.keyword", corpid));
        customerBoolQueryBuilder.filter(termsQuery("del", Arrays.asList(DelEnum.NORMAL.getDel(), DelEnum.DELETE.getDel())));
        customerBoolQueryBuilder.filter(termsQuery(CustomerManagementEnum.getEsAttr4Keyword(CustomerManagementEnum.CLUE_ID), clueIdList));
        customerBoolQueryBuilder.filter(termQuery(ElasticConstant.MY_JOIN_FIELD, IndexTypeEnum.IDX_SAAS_CUSTOMER.getType()));
        customerBoolQueryBuilder.filter(termsQuery("flowStatus", FlowStatusEnum.getStatisticType()));
        return customerBoolQueryBuilder;
    }

    /**
     * 组装线索查询条件
     * date: 2022/3/16 1:49 下午
     *
     * @param corpid:
     * @param marketActivityDataId:
     * @author yingjf
     */
    private BoolQueryBuilder packeageCluesBoolQueryBuilder(String corpid, Long marketActivityDataId) throws XbbException {
        BoolQueryBuilder cluesBoolQueryBuilder = boolQuery();
        cluesBoolQueryBuilder.filter(termQuery("corpid.keyword", corpid));
        cluesBoolQueryBuilder.filter(termQuery("del", 0));
        cluesBoolQueryBuilder.filter(termQuery(ClueEnum.getEsAttr4Keyword(ClueEnum.MARKET_ACTIVITY_ID), marketActivityDataId));
        cluesBoolQueryBuilder.filter(termQuery(ElasticConstant.MY_JOIN_FIELD, IndexTypeEnum.IDX_SAAS_CLUE.getType()));
        return cluesBoolQueryBuilder;
    }

    private BoolQueryBuilder packeageCluesBoolQueryBuilderIncludeRecycle(String corpid, Long marketActivityDataId) throws XbbException {
        BoolQueryBuilder cluesBoolQueryBuilder = boolQuery();
        cluesBoolQueryBuilder.filter(termQuery("corpid.keyword", corpid));
        cluesBoolQueryBuilder.filter(termQuery(ClueEnum.getEsAttr4Keyword(ClueEnum.MARKET_ACTIVITY_ID), marketActivityDataId));
        cluesBoolQueryBuilder.filter(termQuery(ElasticConstant.MY_JOIN_FIELD, IndexTypeEnum.IDX_SAAS_CLUE.getType()));
        cluesBoolQueryBuilder.filter(termsQuery("del", Arrays.asList(DelEnum.NORMAL.getDel(), DelEnum.DELETE.getDel())));
        return cluesBoolQueryBuilder;
    }


    private List<PaasFormDataEntityExt> getMarketActivityList(String corpid, List<Long> marketIds) throws XbbException {
        BoolQueryBuilder boolQueryBuilder = boolQuery();
        boolQueryBuilder.filter(termQuery("corpid.keyword", corpid));
        boolQueryBuilder.filter(termQuery("del", 0));
        if (Objects.nonNull(marketIds) && marketIds.size() > 0) {
            boolQueryBuilder.filter(termsQuery("dataId", marketIds));
        }
        boolQueryBuilder.filter(termsQuery("flowStatus", FlowStatusEnum.getStatisticType()));
        boolQueryBuilder.filter(termQuery(ElasticConstant.MY_JOIN_FIELD, IndexTypeEnum.IDX_SAAS_MARKET_ACTIVITY.getType()));
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.query(boolQueryBuilder);
        SearchRequest searchRequest = new SearchRequest(IndexTypeEnum.IDX_SAAS_MARKET_ACTIVITY.getIndex());
        searchRequest.source(sourceBuilder);
        EsUtil.setPage(sourceBuilder, 1, SizeConstant.PAGE_SIZE);
        List<PaasFormDataEntityExt> marketActivityList = xbbElasticsearchRestTemplate.findScrollList(PaasFormDataEntityExt.class, searchRequest, SizeConstant.SCROLL_TIME_IN_MILLIS);
        return marketActivityList;
    }

    /**
     * 同步已经公司的合同
     * date: 2022/3/16 10:42 上午
     *
     * @param corpid:
     * @author yingjf
     */
    private void syncContract(String corpid, Integer startTime, Integer endTime) throws XbbException {
        // 需要处理的客户
        Set<Long> customerIdSet = new HashSet<Long>();
        // 已经处理的合同 需要过滤一下  防止重复同步
        Set<Long> contractIdSet = new HashSet<Long>();
        Map<String, Object> params = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        params.put("corpid", corpid);
        // fix 需要将删除客户的合同也进行同步，回测客户回收站的客户  删除的客户没合同  主要是同步线索在回收站的时候
        // params.put("del", 0);
        // fix 转成distinct  ，减少数据量  下面需要就先不转了
        List<StaticForMarketActivityEntity> customerIdNoDeduplication = marketActivityForStaticModel.findEntitys(params);
        if (CollectionsUtil.isEmpty(customerIdNoDeduplication)) {
            // 都没客户 说明都不是由线索转的客户建的合同
            return;
        }
        for (StaticForMarketActivityEntity temp : customerIdNoDeduplication) {
            if (Objects.nonNull(temp.getCustomerId())) {
                customerIdSet.add(temp.getCustomerId());
            }
            if (Objects.nonNull(temp.getContractId())) {
                contractIdSet.add(temp.getContractId());
            }
        }

        Long totalCount = getContractTotalCount(corpid, startTime, endTime, customerIdSet, contractIdSet);
        log.info("公司: {} 处理合同数: {}", corpid, totalCount);
        IndexTypeEnum indexTypeEnum = IndexTypeEnum.IDX_SAAS_CONTRACT;
        BoolQueryBuilder boolQueryBuilder = getBaseBoolQueryBuilder(corpid, indexTypeEnum);
        RangeQueryBuilder addTimeQuery = null;
        if (Objects.nonNull(startTime)) {
            addTimeQuery = rangeQuery("addTime");
            addTimeQuery.gte(startTime);
        }
        if (Objects.nonNull(endTime)) {
            if(addTimeQuery == null){
                addTimeQuery = rangeQuery("addTime");
            }
            addTimeQuery.lt(endTime);
        }
        if(addTimeQuery != null){
            boolQueryBuilder.filter(addTimeQuery);
        }
        // fix 如果contractIdSet为空会是什么样的结果
        if (CollectionsUtil.isNotEmpty(contractIdSet)) {
            boolQueryBuilder.filter(boolQuery().mustNot(termsQuery("id", contractIdSet)));
        }
        boolQueryBuilder.filter(termsQuery(ContractEnum.getEsAttr4Keyword(ContractEnum.LINK_CUSTOMER), customerIdSet));
        // 排除红冲合同
        boolQueryBuilder.filter(termQuery(ContractEnum.getEsAttr4Keyword(ContractEnum.IS_RED), 0));
        boolQueryBuilder.filter(termsQuery("flowStatus", FlowStatusEnum.getStatisticType()));
        for (int i = 0; i <= totalCount / 100; i++) {
            SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
            SearchRequest searchRequest = new SearchRequest(indexTypeEnum.getIndex());
            sourceBuilder.query(boolQueryBuilder);
            searchRequest.source(sourceBuilder);
            // fix 分页失效，需要将searchRequest、SearchSourceBuilder的初始化动作放进循环，最好调用paasEsModel或者PaasFormDataEsModel的查询接口
            PageRequest pageRequest = EsUtil.setPage(sourceBuilder, i + 1, 100);
            // todo 测试下看看会不会有重复的es语句
            XbbAggregatedPage<PaasFormDataEntity> esEntities = xbbElasticsearchRestTemplate.queryForPages(pageRequest, searchRequest, PaasFormDataEntity.class);
            // 合同
            List<PaasFormDataEntity> list = esEntities.getContent();
            // todo 加个continue
            // fix: 2022/3/16 调的新增合同的逻辑  走批量处理
            marketActivityForStaticHelp.addBatchContractByScript(corpid, list, customerIdNoDeduplication);
        }
    }

    /**
     * 获取一家公司的合同总数
     * date: 2022/3/16 10:42 上午
     *
     * @param corpid:
     * @author yingjf
     */
    private long getContractTotalCount(String corpid, Integer startTime, Integer endTime, Set<Long> customerIdSet, Set<Long> contractIdSet) throws XbbException {
        IndexTypeEnum indexTypeEnum = IndexTypeEnum.IDX_SAAS_CONTRACT;
        BoolQueryBuilder boolQueryBuilder = getBaseBoolQueryBuilder(corpid, indexTypeEnum);
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        SearchRequest searchRequest = new SearchRequest(indexTypeEnum.getIndex());
        boolQueryBuilder.filter(termsQuery("flowStatus", FlowStatusEnum.getStatisticType()));
        boolQueryBuilder.filter(termsQuery(ContractEnum.getEsAttr4Keyword(ContractEnum.LINK_CUSTOMER), customerIdSet));
        if (CollectionsUtil.isNotEmpty(contractIdSet)) {
            boolQueryBuilder.filter(boolQuery().mustNot(termsQuery("id", contractIdSet)));
        }
        RangeQueryBuilder addTimeQuery = null;
        if (Objects.nonNull(startTime)) {
            addTimeQuery = rangeQuery("addTime");
            boolQueryBuilder.filter(addTimeQuery.gte(startTime));
        }
        if (Objects.nonNull(endTime)) {
            if(addTimeQuery == null){
                addTimeQuery = rangeQuery("addTime");
            }
            addTimeQuery.lt(endTime);
        }
        if(addTimeQuery != null){
            boolQueryBuilder.filter(addTimeQuery);
        }
        // 排除红冲合同
        boolQueryBuilder.filter(termQuery(ContractEnum.getEsAttr4Keyword(ContractEnum.IS_RED), 0));
        sourceBuilder.query(boolQueryBuilder);
        searchRequest.source(sourceBuilder);
        long count = xbbElasticsearchRestTemplate.count(searchRequest, PaasFormDataEntityExt.class);
        return count;
    }

    public BoolQueryBuilder getBaseBoolQueryBuilder(String corpid, IndexTypeEnum indexTypeEnum) {
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        boolQueryBuilder.filter(termQuery("corpid.keyword", corpid));
        boolQueryBuilder.filter(termQuery("del", DelEnum.NORMAL.getDel()));
        boolQueryBuilder.filter(termQuery(ElasticConstant.MY_JOIN_FIELD, indexTypeEnum.getType()));
        return boolQueryBuilder;
    }

    private StaticForMarketActivityEntity setEntityData(String corpid, PaasFormDataEntityExt marketActivity, Long nowDate) throws XbbException {
        StaticForMarketActivityEntity staticForMarketActivityEntity = new StaticForMarketActivityEntity();
        staticForMarketActivityEntity.setCorpid(corpid);
        staticForMarketActivityEntity.setMarketId(marketActivity.getDataId());
        staticForMarketActivityEntity.setMarketType(FastJsonHelper.getStringFromFormData(marketActivity.getData(), MarketActivityEnum.GENRE.getAttr()));
        staticForMarketActivityEntity.setMarketName(FastJsonHelper.getStringFromFormData(marketActivity.getData(), MarketActivityEnum.NAME.getAttr()));
        staticForMarketActivityEntity.setMarketEndTime(FastJsonHelper.getLongFromFormData(marketActivity.getData(), MarketActivityEnum.END_TIME.getAttr()));
        staticForMarketActivityEntity.setActualCost(FastJsonHelper.getDoubleFromFormData(marketActivity.getData(), MarketActivityEnum.REAL_COST.getAttr()));
        staticForMarketActivityEntity.setDataId(0L);
        staticForMarketActivityEntity.setAddTime(nowDate);
        staticForMarketActivityEntity.setUpdateTime(nowDate);
        staticForMarketActivityEntity.setDel(0);
        return staticForMarketActivityEntity;
    }
}
